﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WebCamView.ServiceReference1;
using WebCamView.Code;
using System.Windows.Media.Imaging;
using System.IO;

namespace WebCamView.Views.Controls
{
    public partial class CamControl : UserControl
    {
        private MetaCamInfo info;

        public MetaCamInfo Info
        {
            get
            {
                return info;
            }
            set
            {

                info = value;
                name.Content = info.Name;
            }
        }
        private Service1Client service;
        private ImageData imageData;

        public CamControl()
        {
            InitializeComponent();
            service = new Service1Client();
            service.GetImageCompleted += new EventHandler<GetImageCompletedEventArgs>(service_GetImageCompleted);
        }

        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            service.GetImageAsync(info);   
        }

        private void service_GetImageCompleted(object sender, GetImageCompletedEventArgs args)
        {
            imageData = args.Result;
            using (MemoryStream ms = new MemoryStream(imageData.Content.ToArray()))
            {
                BitmapImage b = new BitmapImage();
                b.SetSource(ms);
                image1.Source = b;
            }
        }
    }
}
